<?php
/**
 * SellerService.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-04-24 16:09:21
 * @modified   2023-04-24 16:09:21
 */

namespace Plugin\MultiSeller\Services;

use Beike\Models\Product;
use Beike\Seller\Repositories\ProductRepo;
use Beike\Seller\Repositories\SellerRepo;

class SellerService
{
    /**
     * 创建一个商户
     *
     * @param array $data // ['seller_group_id', 'product_approve', 'status', 'locale', 'email', 'password', 'store_name', 'seller_user_group_id']
     * @return mixed
     */
    public static function create(array $data)
    {
        $data['seller_user_group_id'] = $data['seller_user_group_id'] ?? plugin_setting('multi_seller.default_seller_user_group_id', 0);
        $seller                       = SellerRepo::create($data);

        return $seller;
    }

    public static function update(int $id, array $data)
    {
        $password    = $data['password'] ?? '';
        if (empty($password)) {
            unset($data['password']);
        }
        $originSeller = SellerRepo::find($id);

        if ($originSeller->status && !$data['status']) {
            ProductRepo::disableProductsBySellerId($id);
        }

        $seller = SellerRepo::update($id, $data);

        return $seller;
    }
}
